var call = require("../../utils/ajax.js")
var that;
var ol = 0;
// var ImgURL = "";
var app = getApp();
var petSelectList = require("../../utils/petSelectList.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        brands: [],
        objectArray: [{
                brand: "福州",
                id: 0,
                array: ['鼓楼区', '台江区', '仓山区', '马尾区', '晋安区', '福清市', '长乐市', '闽侯县', '连江县', '罗源县', '闽清县', '永泰县']
            },
            {
                brand: "厦门",
                id: 1,
                array: ['思明区', '海沧区', '湖里区', '集美区', '同安区', '翔安区']
            },
            {
                brand: "漳州",
                id: 2,
                array: ['芗城区', '龙文区', '龙海市', '漳浦县', '南靖县', '云霄县', '平和县', '华安县', '长泰县', '诏安县', '东山县']
            },
            {
                brand: "龙岩",
                id: 3,
                array: ['新罗区', '漳平市', '长汀县', '永定县', '上杭县', '武平县', '连城县']
            },
            {
                brand: "宁德",
                id: 4,
                array: ['蕉城区', '东侨新区', '福安市', '福鼎市', '霞浦县', '古田县', '屏南县', '寿宁县', '周宁县', '柘荣县']
            },
            {
                brand: "平潭",
                id: 5,
                array: ['潭城', '苏澳', '流水', '澳前', '北厝', '平原', '敖东']
            },
            {
                brand: "泉州",
                id: 6,
                array: ['丰泽区', '鲤城区', '洛江区', '泉港区', '石狮市', '晋江市', '南安市', '惠安县', '安溪县', '永春县', '德化县', '金门县']
            },
            {
                brand: "三明",
                id: 7,
                array: ['梅列区', '三元区', '永安市', '明溪县', '清流县', '宁化县', '大田县', '尤溪县', '沙县', '将乐县', '泰宁县', '建宁县']
            },
            {
                brand: "莆田",
                id: 8,
                array: ['城厢区', '涵江区', '荔城区', '秀屿区', '仙游县']
            },
            {
                brand: "南平",
                id: 8,
                array: ['延平区', '邵武市', '武夷山市', '建瓯市', '建阳市', '顺昌县', '浦城县', '光泽县', '松溪县', '政和县']
            }
        ],
        object: [],
        brandindex: 0,
        index1: 0,
        jueyu: [{
            "id": "1",
            "text": "已绝育"
        }, {
            "id": "0",
            "text": "未绝育"
        }],
        jueyu1: [{
            "id": "1",
            "text": "已驱虫"
        }, {
            "id": "0",
            "text": "未驱虫"
        }],
        jueyu2: [{
            "id": "1",
            "text": "已打疫苗"
        }, {
            "id": "0",
            "text": "未打疫苗"
        }],
        currentData: 0,
        catList: [],
        doglList: [],
        petItems: [{
                name: '喵星人',
                value: '喵星人',
            },
            {
                name: '汪星人',
                value: '汪星人',
            }
        ],
        sexItems: [{
                name: '男孩子',
                value: '男孩子',
            },
            {
                name: '女孩子',
                value: '女孩子',
            },
        ],
        images: [],
        uploadedImages: [],
        ImgURL: "",
        pet_name: "",
        pet_age: "",
        pet_about_me: "",
        pet_adopt_condition: "",
        contacts_wechat: "",
        contacts_phone: "",
        city_area: "",
        county_area: "",
        detailed_area: "",
        publisher_id: "",

    },
    // 是否绝育
    getDate1: function(e) {
        console.log(e.detail.text)
        var that = this;
        that.setData({
            pet_sterilization: e.detail.text
        });
        // this.onLoad_list(e.detail.text);
    },
    // 是否驱虫
    getDate2: function(e) {
        console.log(e.detail.text)
        var that = this;
        that.setData({
            pet_insects: e.detail.text
        });
        // this.onLoad_list(e.detail.text);
    },
    // 是否打疫苗
    getDate3: function(e) {
        console.log(e.detail.text)
        var that = this;
        that.setData({
            pet_vaccine: e.detail.text
        });
        // this.onLoad_list(e.detail.text);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        try {
            wx.removeStorageSync('fileName')
           } catch (e) {
                // Do something when catch error 
               }
               if(!app.globalData.userState){
                wx.showToast({
                    title: '您未登录！',
                    icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
                    duration: 2000     
                  }) 
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1, // 回退前 delta(默认为1) 页面
                    })
                }, 1000)
            }
        ol = 0;
        let publisher_id = "";
        publisher_id = wx.getStorageSync('openID');
        this.setData({
            publisher_id: publisher_id,
        })
        let catList = petSelectList.catList();
        let doglList = petSelectList.doglList();
        this.setData({
            catList: catList,
            doglList: doglList
        })
        var objectArray = this.data.objectArray
        var brands = []
        for (var i = 0; i < objectArray.length; i++) {
            brands.push(objectArray[i].brand)
        }
        this.setData({
            brands: brands,
            object: objectArray[this.data.brandindex].array,
            city_area: objectArray[this.data.brandindex].brand,
            county_area: this.data.objectArray[this.data.brandindex].array[this.data.index1]
        })
    },
    bindPickerChangecity: function(e) {
        this.setData({
            brandindex: e.detail.value,
            index1: 0
        })
        var objectArray = this.data.objectArray
        this.setData({
            object: objectArray[this.data.brandindex].array
        })

        // console.log(this.data.brands[this.data.brandindex])
        this.setData({
            city_area: this.data.brands[this.data.brandindex]
        })
        console.log(this.data.city_area)

        // console.log(this.data.brandindex)
        //     console.log(objectArray[this.data.brandindex].array[this.data.index1])
    },
    bindPickerChangearea: function(e) {
        this.setData({
            index1: e.detail.value
        })
        this.setData({
                county_area: this.data.objectArray[this.data.brandindex].array[this.data.index1]
            })
            // console.log(this.data.objectArray[this.data.brandindex].array[this.data.index1])
        console.log(this.data.county_area)
    },
    radio: function(e) {

        var index = e.currentTarget.dataset.index; //获取当前点击的下标
        var petItems = this.data.petItems; //选项集合
        var sexItems = this.data.sexItems;
        if (petItems[index].checked) return; //如果点击的当前已选中则返回
        petItems.forEach(item => {
            item.checked = false
        })
        petItems[index].checked = true; //改变当前选中的checked值
        this.setData({
            petItems: petItems
        });


    },
    bindPickerChange: function(e) {
        var that = this;
        that.setData({
            value: that.data.catList[e.detail.value].name,
            Index: e.detail.value
        })
        console.log('picker发送选择改变，携带值为', that.data.catList[e.detail.value].name)
    },
    bindPickerChange1: function(e) {
        var that = this;
        that.setData({

            value: that.data.doglList[e.detail.value].name,
            Index: e.detail.value
        })
        console.log('picker发送选择改变，携带值为', that.data.doglList[e.detail.value].name)
    },
    radio_sex: function(e) {
        var index = e.currentTarget.dataset.index;
        var sexItems = this.data.sexItems;
        if (sexItems[index].checked) return; //如果点击的当前已选中则返回
        sexItems.forEach(item => {
            item.checked = false
        })
        sexItems[index].checked = true; //改变当前选中的checked值
        this.setData({
            sexItems: sexItems
        });
    },

    radioChange: function(e) {
        var checkValue = e.detail.value;
        this.setData({
            checkValue: checkValue
        });
        console.log(checkValue)
        var that = this
        if (checkValue == '喵星人') {
            that.setData({
                hideAdd: 0
            })
        } else {
            that.setData({
                hideAdd: 1
            })
        }

    },
    radioChange_sex: function(e) {
        var checkValue_sex = e.detail.value;
        this.setData({
            checkValue_sex: checkValue_sex
        });
        console.log(checkValue_sex)
    },
    sendImg: function(imgurl) {},
    chooseImage: function() {
        // 选择图片
        var that = this
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths;
                let kla = false;

                function admin(tempFilePaths) {
                    console.log("ss");
                    ol = ol + 1;
                    if (ol == 3) {
                        console.log("ss")
                        kla = true
                    } else {
                        console.log("ss")
                        kla = false
                    }
                    return tempFilePaths;
                }
                let ddd = admin(tempFilePaths);
                console.log(ol);
                that.setData({
                    images: that.data.images.concat(ddd),
                    kk: kla
                });
            }
        })

    },
    sendSearch: function(data) {
        call.request1(url, postData, function(res) {

        }, function(e) {

        })

    },
    test: function() {
        let kk = this.data.images;
        this.sendImg(kk);
    },
    // 图片预览
    previewImage: function(e) {
        //console.log(this.data.images);
        var current = e.target.dataset.src
        wx.previewImage({
            current: current,
            urls: this.data.images
        })
    },
    delete: function(e) {
        let kk = this;
        var index = e.currentTarget.dataset.index;
        var images = this.data.images;
        images.splice(index, 1);
        this.setData({
            images: images
        });
        ol = ol - 1;
        if (ol != 3) {
            kk.setData({
                kk: false
            })
        }
        console.log(ol)
    },
    up: function() {

        console.log("执行到了")
        let FileName = "";
        FileName = wx.getStorageSync('fileName');
        console.log(FileName)
        let that = this;
        let kk = this.data.images;
        let url = 'Img/addImg?className=pet&fileName=null';
        console.log(kk)
        console.log(url)
        let pushData = that.make();
        let data = {
            url: url, //这里是你要上传的服务器接口
            path: kk, //这里是你最开始定义的图片数组
            data: {
                pushURL: "Pet/addPet",
                pushData: pushData,//这是个数据传参
                pushDataChange: function (pushData,FileName) {
                    console.log(FileName);
                    pushData.pet_picture = FileName;
                    return pushData
                },//这是个传参函数
                success: function(res) {
                    console.log(res)
                    if (res.message === "操作成功") {
                        wx.hideLoading();
                        wx.showToast({
                            title: '提交成功',
                            icon: 'success', //如果要纯文本，不要icon，将值设为'none'
                            duration: 1000
                        })
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1, // 回退前 delta(默认为1) 页面
                            })
                        }, 1000)
                    } else {
                        wx.hideLoading();
                        console.log(res)
                        wx.showToast({
                            title: '提交失败,请重试！',
                            icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                            duration: 3000
                        })
                    }
                },
                fail: function(e) {
                    wx.showToast({
                        title: '未知错误,请重试！',
                        icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                        duration: 3000
                    })
                    console.log(e) //发布失败后会执行此函数
                }
            }
        }
        call.uploadImage(data);
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        // ol = 0;
        console.log("执行了")

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },
    // getMessage: function() {
    //     this.search_pet_age();
    //     this.search_pet_name();
    //     this.search_pet_message();
    //     this.search_pet_contact();
    //     this.search_pet_area();
    // },
    submit: function() {
        let hh = this;
        wx.showModal({
            title: '提示',
            content: '确认是否提交',
            success: function(res) {
                if (res.confirm) {
                    if (hh.data.images != "" && hh.data.pet_name != "" && hh.data.pet_age != "" && hh.data.pet_about_me != "" && hh.data.pet_adopt_condition != "" && hh.data.contacts_wechat != "" && hh.data.contacts_phone != "" && hh.data.city_area != "" && hh.data.county_area != "" && hh.data.detailed_area != "") {
                        hh.up();
                        wx.showLoading({
                            title: '正在提交申请',
                        })
                    } else {
                        wx.showModal({
                            title: "温馨提示",
                            content: "请正确填写宠物信息并至少上传一张宠物图片",
                            success: function(res) {
                                if (res.confirm) {

                                } else if (res.cancel) {

                                }
                            }

                        })
                    }

                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })



    },

    pet_name: function(e) {
        this.setData({
            pet_name: e.detail.value
        })
    },
    pet_age: function(e) {
        this.setData({
            pet_age: e.detail.value
        })
    },
    pet_about_me: function(e) {
        this.setData({
            pet_about_me: e.detail.value
        })
    },
    pet_adopt_condition: function(e) {
        this.setData({
            pet_adopt_condition: e.detail.value
        })
    },
    contacts_wechat: function(e) {
        this.setData({
            contacts_wechat: e.detail.value
        })
    },
    contacts_phone: function(e) {
        this.setData({
            contacts_phone: e.detail.value
        })
    },
    // city_area: function(e) {
    //     this.setData({
    //         city_area: e.detail.value
    //     })
    // },
    // county_area: function(e) {
    //     this.setData({
    //         county_area: e.detail.value
    //     })
    // },
    detailed_area: function(e) {
        this.setData({
            detailed_area: e.detail.value
        })
    },
    // pet_sterilization: function(e) {
    //     this.setData({
    //         pet_sterilization: e.detail.value
    //     })
    // },
    // pet_insects: function(e) {
    //     this.setData({
    //         pet_insects: e.detail.value
    //     })
    // },
    // pet_vaccine: function(e) {
    //     this.setData({
    //         pet_vaccine: e.detail.value
    //     })
    // },
    make: function() {
        let admin = this;
        let SearchM = {
            // "city_area": "福州",
            // "contacts_phone": 18359926826,
            // "contacts_wechat": "c122355283",
            // "county_area": "鼓楼区",
            // "detailed_area": "白马新区501号",
            // "pet_about_me": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean euismod bibendum laoreet. Proin gravida dolor sit amet lacus accumsan et viverra justo commodo. Proin sodales pulvinar sic tempor. Sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nam fermentum, nulla luctus pharetra vulputate, felis tellus mollis orci, sed rhoncus pronin sapien nunc accuan eget.",
            // "pet_adopt_condition": "按时打疫苗，不得遗弃、转让、贩卖他人、虐待...\n\n科学养宠，溜宠牵引\n\n爱心善缘，不忘初心",
            "pet_adopt_process": "点击“领养我”联系管理员-到基地带我回家",
            // "pet_age": "5岁",
            // "pet_category": "汪星人",
            // "pet_insects": "已驱虫",
            // "pet_name": "小红",
            // "pet_sex": "男",
            "pet_state": "待领养",
            // "pet_sterilization": "已绝育",
            // "pet_vaccine": "已打疫苗",
            // "pet_varieties": "金毛",
            // "publisher_id": "oIxLF5PubOflqkJfjG4_9x-qIIWk",
            // "publisher_name": "爱宠协会"

        };
        SearchM.publisher_id = admin.data.publisher_id;
        SearchM.pet_name = admin.data.pet_name;
        SearchM.pet_sex = admin.data.checkValue_sex;
        SearchM.pet_category = admin.data.checkValue;
        SearchM.pet_varieties = admin.data.value;
        SearchM.pet_age = admin.data.pet_age;
        SearchM.pet_about_me = admin.data.pet_about_me;
        SearchM.pet_adopt_condition = admin.data.pet_adopt_condition;
        SearchM.contacts_wechat = admin.data.contacts_wechat;
        SearchM.contacts_phone = admin.data.contacts_phone;
        SearchM.city_area = admin.data.city_area;
        SearchM.county_area = admin.data.county_area;
        SearchM.detailed_area = admin.data.detailed_area;
        SearchM.pet_sterilization = admin.data.pet_sterilization;
        SearchM.pet_insects = admin.data.pet_insects;
        SearchM.pet_vaccine = admin.data.pet_vaccine;
        SearchM.pet_picture = "null";
        // this.sendSearch(SearchM);
        console.log(SearchM)
        return SearchM;
    },


    /**
     * 用户点击右上角分享
     */


})